//
//  AppDelegate.swift
//  newSwift
//
//  Created by MAC on 2023/10/17.
//

import UIKit
import SwifterSwift
import Toast_Swift
import SwiftDate
import RxCocoa
import RxSwift
		
@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    // 窗口
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        window = UIWindow()
        window?.backgroundColor = .white
        
        // 根控制器
        switchRootViewController()
        
        // 显示窗口
        window?.makeKeyAndVisible()

        setupNotification()
        
        setupToast()
        
      _ =  BluetoothManager.shared

        return true
    }

    func setupToast() {
        ToastManager.shared.duration = 2.0
        ToastManager.shared.position = .center
    }
    
    func setupNotification() {
        NotificationCenter.default.addObserver(self, selector: #selector(switchRootViewController), name: NSNotification.Name(rawValue: "SwicthRootViewControllerNotification"), object: nil)

    }
}



// MARK: - 设置根控制器
extension AppDelegate {
    
   @objc private func switchRootViewController() {
        
       let isLogin : Bool = UserDefaults().bool(forKey: "isLogin")
       
       if  ((window?.rootViewController) != nil) {
           var rootVc : UIViewController?
           if isLogin == true { // 已经登录
               rootVc = BaseTabbarVC()
           } else { // 未登录
               rootVc = LoginViewController()
           }
           
           window?.switchRootViewController(to: rootVc!)
       }else{
           if isLogin == true { // 已经登录
               window?.rootViewController = BaseTabbarVC()
           } else { // 未登录
               window?.rootViewController = LoginViewController()
           }
       }
    

    }
    
    
    
  
}
